package com.itboye.ihomebank.activity.homefragment;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Layout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.activity.home.ActivityJinJiRenFangYuan;
import com.itboye.ihomebank.activity.home.ActivityShenQingZuFang;
import com.itboye.ihomebank.activity.login.LoginActivity;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.HouseDetailBean;
import com.itboye.ihomebank.bean.HouseDetailBean.Neighbouring;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.CarouselView;
import com.itboye.ihomebank.custom.ExpandTextView;
import com.itboye.ihomebank.custom.RoundImageView;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.CameraPopupWindow;
import com.itboye.ihomebank.util.LoweImageView;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.XImageLoader;
import com.tencent.connect.share.QQShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 房子详情
 */
public class HouseDetailActivity extends BaseOtherActivity implements Observer {
    View v_statusbar;
    LinearLayout house_contnainer;
    TextView lijiqianyue, yuyue, add_shap_title_tv;
    ImageView close_icon, img_setting, img_other;
    MapView mapView;
    private BaiduMap baiduMap;
    private double lat, lng;
    private float radius;
    private MyLocationData locData;
    TextView collection;
    private HousePresenter housepresenter;
    private String status;
    private String favoriteId;
    private String fType;
    private String uid;
    private String houseNo;
    CarouselView img;
    private TextView house_name;
    private TextView add;
    private TextView house_price;
    private TextView txt_huxing;
    private TextView txt_foor;
    private TextView txt_no;
    private HouseDetailBean bean;
    int isCollection = 0;
    private TextView txt_chuzuleixing;
    private TextView txt_tese;
    LinearLayout deviceContainer;
    private TextView txt_area;
    private TextView txt_chaoxiang;
    private AlertDialog alertDialog;
    public static int screenWidth;
    RelativeLayout near;
    HashMap<String, Bitmap> hashMap = new HashMap<>();
    private Tencent mTencent;
    LoweImageView tubiao;
    private String community_id;
    private CameraPopupWindow mCameraPopupWindow;
    LinearLayout tongxiaoqu;
    ExpandTextView addrdetail;// abstract
    private LinearLayout biaoqian;
    ImageView more;
    RelativeLayout layout_collection;
    ImageView collection_img;//收藏的图片
    @Override
    protected int layout_id() {
        // TODO Auto-generated method stub
        SDKInitializer.initialize(getApplicationContext());
        return R.layout.activity_house_detail;
    }

    @Override
    protected void onInitController() {
        // TODO Auto-generated method stub

    }

    // 三个状态实现地图生命周期管理
    @Override
    protected void onDestroy() {
        // 退出时销毁定位
        // locationClient.stop();

        try{
            baiduMap.setMyLocationEnabled(false);
            // TODO Auto-generated method stub
            super.onDestroy();
            mapView.onDestroy();
            mapView = null;
        }catch (Exception e){}
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        // initLocation();
    }

    @Override
    protected void onInit() {
        // TODO Auto-generated method stub
        setStatusBarHeight(v_statusbar);
        // 注意该方法要再setContentView方法之前实现
        mTencent = Tencent
                .createInstance(NetPublicConstant.TENGXUN_APPID, this);
        showProgressDialog("努力加载中,请稍后...", false);
        screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        radius = getIntent().getFloatExtra("radius", 0);
        lat = getIntent().getDoubleExtra("lat", 0.000000);
        lng = getIntent().getDoubleExtra("lng", 0.000000);
        uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
        fType = "1";

        housepresenter = new HousePresenter(this);
        houseNo = getIntent().getStringExtra("house_no");
        favoriteId = houseNo;
        housepresenter.queryHouseDetail(houseNo, uid);
        baiduMap = mapView.getMap();
        add_shap_title_tv.setText("找房");
        img_setting.setVisibility(View.GONE);
        // img_setting.setBackgroundResource(R.drawable.jingjiren2);
        img_other.setVisibility(View.VISIBLE);
        img_other.setBackgroundResource(R.drawable.share01);

        hashMap.put("床",
                BitmapFactory.decodeResource(getResources(), R.drawable.chuang));
        hashMap.put("书桌", BitmapFactory.decodeResource(getResources(),
                R.drawable.shuzhuo));
        hashMap.put("彩电", BitmapFactory.decodeResource(getResources(),
                R.drawable.dianshi));
        hashMap.put("沙发", BitmapFactory.decodeResource(getResources(),
                R.drawable.shafa));
        hashMap.put("冰箱",
                BitmapFactory.decodeResource(getResources(), R.drawable.bingxiang));
        hashMap.put("洗衣机",
                BitmapFactory.decodeResource(getResources(), R.drawable.xiyiji));
        hashMap.put("空调", BitmapFactory.decodeResource(getResources(),
                R.drawable.kongtiao));

        hashMap.put("热水器", BitmapFactory.decodeResource(getResources(),
                R.drawable.reshuiqi));
        hashMap.put("餐桌", BitmapFactory.decodeResource(getResources(),
                R.drawable.canzhuo));

        hashMap.put("微波炉", BitmapFactory.decodeResource(getResources(),
                R.drawable.weibolu));

        hashMap.put("椅子", BitmapFactory.decodeResource(getResources(),
                R.drawable.yizi));
        hashMap.put("衣柜", BitmapFactory.decodeResource(getResources(),
                R.drawable.yigui));
    }

    @Override
    protected void onReadIntent(Bundle bundle) {
        // TODO Auto-generated method stub

    }

    boolean isMore = false;

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent = null;
        switch (v.getId()) {
            case R.id.lijiqianyue:// 立即签约
                if (SPUtils.get(this, null, SPContants.USER_ID, "").equals("")) {
                    startActivity(new Intent(this, LoginActivity.class));

                } else if (SPUtils.get(this, null, SPContants.IdentityValidate, "")
                        .equals("0")
                        || SPUtils.get(this, null, SPContants.IdentityValidate, "")
                        .equals("0")) {
                    ByAlert.alert("请先实名制");
                } else if (SPUtils.get(this, null, SPContants.IdentityValidate, "")
                        .equals("2")) {
                    ByAlert.alert("实名制审核中");
                } else if (SPUtils.get(this, null, SPContants.BrokerValidate, "").equals("1")) {
                    ByAlert.alert("经纪人不能申请签约或租房");
                } else {
                    intent = new Intent(this, ActivityShenQingZuFang.class);
                    intent.putExtra("house_no", bean.getHouseNo());
                    startActivity(intent);
                }
                break;
            case R.id.close_icon:
                finish();
                break;
            case R.id.more:
                boolean b = addrdetail.getExpandableStatus();
                b = !b;
                if (b) {
                    more.setImageResource(R.drawable.less);
                } else {
                    more.setImageResource(R.drawable.more_unfold);
                }
                addrdetail.setExpandable(b);
//                if(isMore) {
//                    addrdetail.setMaxLines(10);
//                }else{
//                    addrdetail.setMaxLines(2);
//                }
                break;
            case R.id.yuyue:// 预约看房
                if (SPUtils.get(this, null, SPContants.USER_ID, "").equals("")) {
                    startActivity(new Intent(this, LoginActivity.class));
                } else {
                    intent = new Intent(this, ActivityJinJiRenFangYuan.class);
                    intent.putExtra("model", bean);
                    startActivity(intent);
                }
//                 if (SPUtils.get(this, null, SPContants.USER_ID, "").equals("")) {
//                 startActivity(new Intent(this, LoginActivity.class));
//                 } else if (SPUtils.get(this, null, SPContants.IdentityValidate,
                // "")
                // .equals("0")
                // || SPUtils.get(this, null, SPContants.IdentityValidate, "")
                // .equals("")) {
                // ByAlert.alert("请先实名制");
                // } else if (SPUtils.get(this, null, SPContants.IdentityValidate,
                // "")
                // .equals("2")) {
                // ByAlert.alert("实名制审核中");
                // } else {
                // intent = new Intent(this, ActivityYuYueKanfang.class);
                // intent.putExtra("house_no", bean.getHouseNo());
                // startActivity(intent);
                // }
                break;
            case R.id.layout_collection:
            case R.id.collection:
                uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
                if (uid.equals("")) {
                    intent = new Intent(this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    showProgressDialog("操作中,请稍后", false);
                    housepresenter.collection(uid, fType, favoriteId,
                            isCollection == 0 ? 1 : 0);
                }
                break;
            case R.id.img_setting:// 查看经纪人
                showJingJiRen();
                break;
            case R.id.img_other:// 分享
                mCameraPopupWindow = new CameraPopupWindow(this, this);
                ((TextView) mCameraPopupWindow.getContentView().findViewById(
                        R.id.open_camera)).setText("QQ");
                mCameraPopupWindow.getContentView().findViewById(R.id.pick_image)
                        .setVisibility(View.GONE);
                mCameraPopupWindow.getContentView().findViewById(R.id.line_1)
                        .setVisibility(View.GONE);

                mCameraPopupWindow.showAtLocation(v, Gravity.BOTTOM
                        | Gravity.CENTER_HORIZONTAL, 0, 0);
                break;
            case R.id.r:
                alertDialog.dismiss();
                // intent = new Intent(this, ActivityJinJiRenFangYuan.class);
                // intent.putExtra("model", bean);
                // startActivity(intent);
                break;
            case R.id.near:
                intent = new Intent(this, ActivityNear.class);
                intent.putExtra("latitude", lat);
                intent.putExtra("longitude", lng);
                startActivity(intent);
                break;
            case R.id.tubiao:// 小区走势
                intent = new Intent(this, ActivityZouShi.class);
                intent.putExtra("community_id",
                        (bean.getCommunityId() == null || "".equals(bean
                                .getCommunityId())) ? "0" : bean.getCommunityId());
                startActivity(intent);
                break;
            case R.id.open_camera:
                shareToQQ();
                mCameraPopupWindow.dismiss();
                break;
            case R.id.camera_cancel_tv:
                mCameraPopupWindow.dismiss();
                break;
            default:
                break;
        }
    }

    private void shareToQQ() {
        if (NetPublicConstant.TENGXUN_APPID.equals("")) {

            return;
        }
        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,
                QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, "住家");
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "住家分享");
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,
                NetPublicConstant.WEB_URL + "/share");
        params.putString(
                QQShare.SHARE_TO_QQ_IMAGE_URL,
                "http://api.open.qq.com/tfs/show_img.php?appid=1105826926&uuid=app1105826926_50_50%7C1048576%7C1479349383.6039");
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "住家");
        // params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, "其他附加功能");
        mTencent.shareToQQ(this, params, new IUiListener() {

            @Override
            public void onError(UiError arg0) {
                // TODO Auto-generated method stub
                ByAlert.alert("分享失败，请重试");
            }

            @Override
            public void onComplete(Object arg0) {
                // TODO Auto-generated method stub
                ByAlert.alert("分享完成");
            }

            @Override
            public void onCancel() {
                // TODO Auto-generated method stub
                ByAlert.alert("已取消");
            }
        });

    }

    void showJingJiRen() {
        alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.show();
        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.alert_jingjiren);
        TextView tv_title = (TextView) window.findViewById(R.id.name);
        if (bean.getHouseType().equals("1")) {
            tv_title.setText("房东：" + bean.getOwnerInfo().getName());
        } else {
            tv_title.setText("经纪人：" + bean.getOwnerInfo().getName());
        }
        RoundImageView head = (RoundImageView) window.findViewById(R.id.head);
        XImageLoader.load(bean.getOwnerInfo().getHead(), head);
        RelativeLayout layout = (RelativeLayout) window.findViewById(R.id.r);
        layout.setOnClickListener(this);
    }

    private void initLocation() {
        // TODO Auto-generated method stub
        // -----------location config ------------
        System.out.println("lng" + lng + "lat" + lat);
        locData = new MyLocationData.Builder().accuracy(radius).direction(100)
                .latitude(lat).longitude(lng).build();
        // // 此处设置开发者获取到的方向信息，顺时针0-360
        baiduMap.setMyLocationData(locData); // 设置定位数据
        LatLng ll = new LatLng(lat, lng);
        MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 16); // 设置地图中心点以及缩放级别
        baiduMap.animateMapStatus(u);
        TextView textView = new TextView(MyApplcation.ctx);
        textView.setText("自定义的覆盖物");
        textView.setTextSize(16);
        try {
            // 将View转换为BitmapDescriptor
            BitmapDescriptor descriptor = BitmapDescriptorFactory
                    .fromBitmap(BitmapFactory.decodeResource(getResources(),
                            R.drawable.weizhi));
            // 设置覆盖物属性，位置、标题、图标。。。
            MarkerOptions markerOptions = new MarkerOptions()
                    .position(new LatLng(lat, lng)).icon(descriptor).title("标题")
                    .zIndex(9).draggable(true);
            // 在地图上添加覆盖物
            mapView.getMap().addOverlay(markerOptions);
        } catch (Exception e) {
        }

    }

    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub
        ResultEntity entity = handlerError(data);
        if (entity.getMsg().equals("未找到房源信息")){
            finish();
            return;
        }
        if (entity != null) {
            if (entity.getEventType() == HousePresenter.collection_success) {
                collection.setText(isCollection == 0 ? "取消收藏" : "收藏");
                collection_img.setBackgroundResource(isCollection == 0 ? R.drawable.shoucang1: R.drawable.shoucang2);
                isCollection = (isCollection == 0 ? 1 : 0);
                ByAlert.alert(entity.getData());
            } else if (entity.getEventType() == HousePresenter.collection_fail) {
                ByAlert.alert(entity.getData());
            } else if (entity.getEventType() == HousePresenter.houseDetail_success) {
                bean = (HouseDetailBean) entity.getData();
                if (bean.getVerify().equals("0")) {
                    lijiqianyue.setEnabled(false);
                    lijiqianyue.setText("未认证");
                } else {
                    lijiqianyue.setEnabled(true);

                    lijiqianyue.setText("申请签约");
                }

                lat = bean.getLat();
                lng = bean.getLng();
                community_id = bean.getCommunity();
                initLocation();
                isCollection = Integer.parseInt(bean.getIsFavorite());
                collection.setText(isCollection == 1 ? "取消收藏" : "收藏");
                collection_img.setBackgroundResource(isCollection == 1 ? R.drawable.shoucang1: R.drawable.shoucang2);
                if (bean != null) {
                    String imgIds = bean.getImgIds();
                    if (imgIds != null) {
                        String[] imgsArray = null;
                        if (!imgIds.contains(",")) {
                            imgsArray = new String[0];
                            imgsArray[0] = imgIds;
                        } else if (imgIds.contains(",")) {
                            imgsArray = imgIds.split(",");
                        }
                        if (imgsArray != null) {
                            ArrayList<ImageView> arrayBmps = new ArrayList<ImageView>();
                            for (int i = 0; i < imgsArray.length; i++) {
                                ImageView imageView = new ImageView(
                                        HouseDetailActivity.this);
                                imageView.setScaleType(ScaleType.FIT_XY);
                                XImageLoader.load(NetPublicConstant.IMAGE_URL
                                        + imgsArray[i] + SPContants.SHUIYIN, imageView);
                                arrayBmps.add(imageView);
                                imageView.setTag(i);
                            }
                            img.setImageBitmaps(arrayBmps);
                            // //让顶部控件获取到焦点这样滚动条就自然停在顶部了
                            img.setFocusable(true);
                            img.setFocusableInTouchMode(true);
                            img.requestFocus();
                        }

                        setHouseInfo(bean);
                        setSheBei(bean);
                        setNeighbouring(bean);
                    }
                }
            } else if (entity.getEventType() == HousePresenter.houseDetail_fail) {
                ByAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    private void setNeighbouring(HouseDetailBean bean) {
        // TODO Auto-generated method stub
        List<Neighbouring> listNeighbour = bean.getNeighbouring();
        if (listNeighbour.size() < 0) {
            tongxiaoqu.setVisibility(View.GONE);
        } else {
            tongxiaoqu.setVisibility(View.VISIBLE);
        }
        for (int i = 0; i < listNeighbour.size(); i++) {
            View view = LayoutInflater.from(this).inflate(
                    R.layout.item_tongxiaoqufangyuan, null);
            ImageView img_icon = (ImageView) view.findViewById(R.id.img_icon);
            TextView txt_addr = (TextView) view.findViewById(R.id.txt_addr);
            TextView money = (TextView) view.findViewById(R.id.money);
            XImageLoader.load(NetPublicConstant.IMAGE_URL
                    + listNeighbour.get(i).getMainImg(), img_icon);
            txt_addr.setText(listNeighbour.get(i).getOrientation() + "·" + listNeighbour.get(i).getArea() + "_"
                    + listNeighbour.get(i).getAreaZone() + "_"
                    + listNeighbour.get(i).getCommunity() + "·"
                    + listNeighbour.get(i).getHouseHall() + "室" + listNeighbour.get(i).getHouseRoom());
            money.setText(Double.parseDouble(listNeighbour.get(i).getRent())
                    / 100 + "元/月");
            android.widget.RelativeLayout.LayoutParams layoutParams = new android.widget.RelativeLayout.LayoutParams(
                    screenWidth,
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);

            android.widget.RelativeLayout.LayoutParams layoutParams02 = new android.widget.RelativeLayout.LayoutParams(
                    screenWidth - 60, (int) ((screenWidth - 60) * 0.5));
            layoutParams.setMargins(30, 10, 30, 10);
            layoutParams02.setMargins(30, 10, 30, 10);
            img_icon.setLayoutParams(layoutParams02);
            // layoutParams.
            // img_icon.setLayoutParams(layoutParams);
            view.setTag(listNeighbour.get(i).getHouseNo());
            house_contnainer.addView(view, layoutParams);
            view.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(HouseDetailActivity.this,
                            HouseDetailActivity.class);
                    intent.putExtra("house_no", v.getTag().toString());
                    startActivity(intent);
                }
            });
        }
    }

    private void setSheBei(HouseDetailBean bean) {
        // TODO Auto-generated method stub

    }

    private void setHouseInfo(HouseDetailBean bean) {
        // TODO Auto-generated method stub
        house_name.setText(bean.getTitle());
        add.setText(bean.getAddressDetail());
        house_price.setText(Double.parseDouble(bean.getRent()) / 100 + "元/月");
        txt_no.setText(bean.getHouseNo());
        txt_foor.setText(bean.getFloorAt() + "/" + bean.getFloorAll());
        txt_huxing.setText(bean.getHouseRoom() + "室" + bean.getHouseHall()
                + "厅" + bean.getToilet() + "卫");
        txt_chuzuleixing.setText(bean.getRentType());
        txt_tese.setText(bean.getTagNames());
        txt_area.setText(bean.getHouseArea() + "㎡");
        txt_chaoxiang.setText(bean.getOrientation());
        addrdetail.setExpandable(false);
        addrdetail.setText(bean.get_abstract());
        Layout l = addrdetail.getLayout();

//        ByAlert.alert(addrdetail.getLines() + ":" +l.getLineCount()+ "?"+addrdetail.getLineCount());
        if (bean.get_abstract() != null) {
            //addrdetail.getLines()完全伸展还需要的行数   addrdetail.getLineCount()：当前实际行数
            if (addrdetail.getMaxLines() < addrdetail.getLineCount()) {
                more.setVisibility(View.VISIBLE);
            } else {
                more.setVisibility(View.GONE);
            }
        }
        StringBuffer str = new StringBuffer();
        if (bean.getVerify().equals("0")) {

        } else if (bean.getVerify().equals("1")) {
            str.append("已认证房源,");
        }
        if (bean.getHouseType().equals("1")) {// 个人房源、
            str.append("个人房源,");
            yuyue.setText("房东");
        } else if (bean.getHouseType().equals("2")) {// 经纪人房源、
            str.append("经纪人房源,");
            if (bean.getEntrustMoney() != null)
                str.append("佣金设置为：" + Double.parseDouble(bean.getEntrustMoney()) / 100 + "元");
            yuyue.setText("经纪人");
        }
        //    biaoqian.setText(Html.fromHtml(str.toString()));
        String[] strs = str.toString().split(",");
        for (int i = 0; i < strs.length; i++) {
            View innerView = LayoutInflater.from(MyApplcation.ctx).inflate(
                    R.layout.item_biaoqian_two, null);
            TextView btn_name = (TextView) innerView
                    .findViewById(R.id.btn_name);
            //   Drawable drawable=DrawableC
            //    btn_name.setCompoundDrawables();
            Drawable drawable = getResources().getDrawable(R.drawable.fangyuanrenzheng);
            drawable.setBounds(0, 0, 40, 40);
            btn_name.setCompoundDrawables(drawable, null, null, null);
            String s = strs[i];
            btn_name.setText(s);
            ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(
                    ViewGroup.MarginLayoutParams.WRAP_CONTENT,
                    ViewGroup.MarginLayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(5, 5, 5, 5);
            biaoqian.addView(innerView, layoutParams);
        }


        if (bean.getFacility().contains(",")) {
            String[] devices = bean.getFacility().split(",");// 设备
            for (int i = 0; i < devices.length; i++) {
                View view = LayoutInflater.from(mContext).inflate(
                        R.layout.item_other, null);
                android.widget.RelativeLayout.LayoutParams lP = new android.widget.RelativeLayout.LayoutParams(
                        screenWidth / 5, 50);
                TextView textView = (TextView) view.findViewById(R.id.text);
                textView.setWidth(screenWidth / 5);
                ImageView img = (ImageView) view.findViewById(R.id.img);
                if (devices[i].contains("床")){
                    img.setImageDrawable(getResources().getDrawable(R.drawable.chuang));
                }else {
                    img.setImageBitmap(hashMap.get(devices[i]));
                }
                textView.setTextColor(mContext.getResources().getColor(
                        R.color.black));
                lP.setMargins(0, 2, 2, 2);
                textView.setText(devices[i]);
                deviceContainer.addView(view);

            }
        } else {
            View view = LayoutInflater.from(mContext).inflate(
                    R.layout.item_other, null);
            android.widget.RelativeLayout.LayoutParams lP = new android.widget.RelativeLayout.LayoutParams(
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
            TextView textView = (TextView) view.findViewById(R.id.text);
            textView.setTextColor(mContext.getResources().getColor(
                    R.color.black));
            ImageView img = (ImageView) view.findViewById(R.id.img);
            if (bean.getFacility().contains("床")){
                img.setImageDrawable(getResources().getDrawable(R.drawable.chuang));
            }else {
                img.setImageBitmap(hashMap.get(bean.getFacility()));
            }
            lP.setMargins(0, 2, 2, 2);
            textView.setText(bean.getFacility());
            deviceContainer.addView(view);
        }
    }
}
